home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 12984 / 12984.xpi / chrome / VideoDownloaderToolbar.jar / content / db.js < prev    next >
Text File  |  2010-01-29  |  14KB  |  425 lines

  1. if(!com) var com={};
  2. if(!com.VidBar) com.VidBar={};
  3.  
  4. com.VidBar.VidDB = function() {
  5.     this.inizialize();
  6. }
  7.  
  8. com.VidBar.VidDB.prototype = {
  9.     DBName : "viddownloadtb.sqlite",
  10.  
  11.     // Pointer to the database
  12.     DBPointer : null,
  13.  
  14.     // Mozilla Storage Service pointer
  15.     StorageService : null,
  16.  
  17.     // Database Connection
  18.     dbConn : null,
  19.  
  20.     inizialize : function() {
  21.         if (this.DBPointer == null) {
  22.             this.DBPointer = Components.classes["@mozilla.org/file/directory_service;1"]
  23.                     .getService(Components.interfaces.nsIProperties).get(
  24.                             "ProfD", Components.interfaces.nsIFile);
  25.             this.DBPointer.append(this.DBName);
  26.         }
  27.  
  28.         if (this.StorageService == null) {
  29.             this.StorageService = Components.classes["@mozilla.org/storage/service;1"]
  30.                     .getService(Components.interfaces.mozIStorageService);
  31.         }
  32.  
  33.         if (this.dbConn == null) {
  34.             // com.VidBar.__d("db file: "+this.DBPointer.path);
  35.             if (this.DBPointer.exists()) {
  36.                 this.dbConn = this.StorageService.openDatabase(this.DBPointer);
  37.             } else {
  38.                 this.dbConn = this.StorageService.openDatabase(this.DBPointer);
  39.                 this.resetDB();
  40.             }
  41.         }
  42.     },
  43.     resetDB : function() {
  44.         if (this.dbConn.tableExists("waitings")) {
  45.             this.dbConn.executeSimpleSQL("drop table waitings");
  46.         }
  47.  
  48.         this.dbConn
  49.                 .executeSimpleSQL("CREATE TABLE [waitings] ([id] INTEGER  PRIMARY KEY AUTOINCREMENT NOT NULL, [guid] TEXT, [mediaUrl] TEXT, [filename] TEXT, [dir] TEXT, [referrer] TEXT, [size] INTEGER DEFAULT 0)");
  50.         this.dbConn
  51.                 .executeSimpleSQL("CREATE INDEX [WaitingIndex] ON [waitings] (guid)");
  52.  
  53.         if (this.dbConn.tableExists("downloadings")) {
  54.             this.dbConn.executeSimpleSQL("drop table downloadings");
  55.         }
  56.         this.dbConn
  57.                 .executeSimpleSQL("CREATE TABLE [downloadings] ([guid] TEXT, [downloadId] INTEGER, [mediaUrl] TEXT, [filename] TEXT, [dir] TEXT, [referrer] TEXT, [size] INTEGER DEFAULT 0, [status] INTEGER DEFAULT 0, [percentComplete] INTEGER DEFAULT 0, [amountTransferred] INTEGER DEFAULT 0, [speed] REAL DEFAULT 0)");
  58.         this.dbConn
  59.                 .executeSimpleSQL("CREATE INDEX [DownloadingIndex] ON [downloadings] (guid)");
  60.  
  61.         if (this.dbConn.tableExists("sites")) {
  62.             this.dbConn.executeSimpleSQL("drop table sites");
  63.         }
  64.         this.dbConn
  65.                 .executeSimpleSQL("CREATE TABLE [sites] ([name] TEXT, [url] TEXT)");
  66.         this.dbConn
  67.                 .executeSimpleSQL("CREATE INDEX [SiteIndex] ON [sites] (name)");
  68.         this.restoreDefaultSites();
  69.     },
  70.     resetOldFormatDB : function() {
  71.         if (this.dbConn.tableExists("waitings")) {
  72.             this.dbConn.executeSimpleSQL("drop table waitings");
  73.         }
  74.         this.dbConn
  75.                 .executeSimpleSQL("CREATE TABLE [waitings] ([id] INTEGER  PRIMARY KEY AUTOINCREMENT NOT NULL, [guid] TEXT, [mediaUrl] TEXT, [filename] TEXT, [dir] TEXT, [referrer] TEXT, [size] INTEGER DEFAULT 0)");
  76.         this.dbConn
  77.                 .executeSimpleSQL("CREATE INDEX [WaitingIndex] ON [waitings] (guid)");
  78.  
  79.         if (this.dbConn.tableExists("downloadings")) {
  80.             this.dbConn.executeSimpleSQL("drop table downloadings");
  81.         }
  82.         this.dbConn
  83.                 .executeSimpleSQL("CREATE TABLE [downloadings] ([guid] TEXT, [downloadId] INTEGER, [mediaUrl] TEXT, [filename] TEXT, [dir] TEXT, [referrer] TEXT, [size] INTEGER DEFAULT 0, [status] INTEGER DEFAULT 0, [percentComplete] INTEGER DEFAULT 0, [amountTransferred] INTEGER DEFAULT 0, [speed] REAL DEFAULT 0)");
  84.         this.dbConn
  85.                 .executeSimpleSQL("CREATE INDEX [DownloadingIndex] ON [downloadings] (guid)");
  86.     },
  87.     checkDBFormat : function() {
  88.         this.inizialize();
  89.  
  90.         var entries = [];
  91.         //var dbStatement = this.dbConn.createStatement("PRAGMA table_info (waitings)");
  92.         var dbStatement = this.dbConn.createStatement("select * from waitings");
  93.         
  94.         
  95.         dbStatement.execute();
  96.         
  97.                 
  98.         var countCol = dbStatement.columnCount;
  99.         
  100.         dbStatement.finalize();
  101.         
  102.         //for new format db table waitings count=7
  103.         if (countCol < 7)
  104.             this.resetOldFormatDB();
  105.     },
  106.     addWaiting : function(guid, mediaUrl, filename, dir, referrer, size) {
  107.         this.inizialize();
  108.  
  109.         var dbStatement = this.dbConn
  110.                 .createStatement("insert into waitings(guid, mediaUrl, filename, dir, referrer, size) values (?1, ?2, ?3, ?4, ?5, ?6)");
  111.         dbStatement.bindStringParameter(0, guid);
  112.         dbStatement.bindStringParameter(1, mediaUrl);
  113.         dbStatement.bindStringParameter(2, filename);
  114.         dbStatement.bindStringParameter(3, dir);
  115.         dbStatement.bindStringParameter(4, referrer);
  116.         dbStatement.bindInt64Parameter(5, size);
  117.  
  118.         dbStatement.execute();
  119.         dbStatement.finalize();
  120.     },
  121.     getWaitings : function() {
  122.         this.inizialize();
  123.  
  124.         var entries = [];
  125.         var dbStatement = this.dbConn
  126.                 .createStatement("select guid, mediaUrl, filename, dir, referrer, size from waitings order by id asc");
  127.  
  128.         while (dbStatement.executeStep()) {
  129.             var entry = {};
  130.             entry["guid"] = dbStatement.getString(0);
  131.             entry["mediaUrl"] = dbStatement.getString(1);
  132.             entry["filename"] = dbStatement.getString(2);
  133.             entry["dir"] = dbStatement.getString(3);
  134.             entry["referrer"] = dbStatement.getString(4);
  135.             entry["size"] = dbStatement.getInt64(5);
  136.             entries.push(entry);
  137.         }
  138.  
  139.         dbStatement.finalize();
  140.  
  141.         return entries;
  142.     },
  143.     removeWaiting : function(guid) {
  144.         this.inizialize();
  145.  
  146.         var dbStatement = this.dbConn
  147.                 .createStatement("delete from waitings where guid = ?1");
  148.         dbStatement.bindStringParameter(0, guid);
  149.  
  150.         dbStatement.execute();
  151.         dbStatement.finalize();
  152.     },
  153.     removeWaitings : function(guids){
  154.         this.inizialize();
  155.  
  156.         var dbStatement = this.dbConn
  157.                 .createStatement("delete from waitings where guid = ?1");
  158.         for(var i=0;i<guids.length;i++){
  159.             var guid = guids[i];
  160.             dbStatement.bindStringParameter(0, guid);
  161.             dbStatement.execute();
  162.         }
  163.         
  164.         dbStatement.finalize();
  165.     },
  166.     removeAllWaitings : function(){
  167.         this.inizialize();
  168.  
  169.         var dbStatement = this.dbConn
  170.                 .createStatement("delete from waitings");
  171.         dbStatement.execute();
  172.         dbStatement.finalize();
  173.     },
  174.     addDownloading : function(guid, mediaUrl, filename, dir, referrer, size, status,
  175.             downloadId) {
  176.         this.inizialize();
  177.  
  178.         var dbStatement = this.dbConn
  179.                 .createStatement("insert into downloadings(guid, mediaUrl, filename, dir, referrer, size, status, downloadId) values (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)");
  180.         dbStatement.bindStringParameter(0, guid);
  181.         dbStatement.bindStringParameter(1, mediaUrl);
  182.         dbStatement.bindStringParameter(2, filename);
  183.         dbStatement.bindStringParameter(3, dir);
  184.         dbStatement.bindStringParameter(4, referrer);
  185.         dbStatement.bindInt64Parameter(5, size);
  186.         dbStatement.bindInt32Parameter(6, status);
  187.         dbStatement.bindInt32Parameter(7, downloadId);
  188.  
  189.         dbStatement.execute();
  190.         dbStatement.finalize();
  191.     },
  192.     getDownloadings : function() {
  193.         this.inizialize();
  194.  
  195.         var entries = [];
  196.         var dbStatement = this.dbConn
  197.                 .createStatement("select guid, mediaUrl, filename, dir, referrer, size, status, percentComplete, amountTransferred, speed, downloadId from downloadings");
  198.  
  199.         while (dbStatement.executeStep()) {
  200.             var entry = {};
  201.             entry["guid"] = dbStatement.getString(0);
  202.             entry["mediaUrl"] = dbStatement.getString(1);
  203.             entry["filename"] = dbStatement.getString(2);
  204.             entry["dir"] = dbStatement.getString(3);
  205.             entry["referrer"] = dbStatement.getString(4);
  206.             entry["size"] = dbStatement.getInt64(5);
  207.             entry["status"] = dbStatement.getInt32(6);
  208.             entry["percentComplete"] = dbStatement.getInt32(7);
  209.             entry["amountTransferred"] = dbStatement.getInt64(8);
  210.             entry["speed"] = dbStatement.getDouble(9);
  211.             entry["downloadId"] = dbStatement.getInt32(10);
  212.             entries.push(entry);
  213.         }
  214.  
  215.         dbStatement.finalize();
  216.  
  217.         return entries;
  218.     },
  219.     getDownloadingByGuid : function(guid) {
  220.         //com.VidBar.__d("DB: getDownloadingByGuid - "+guid);
  221.         this.inizialize();
  222.  
  223.         var entry = null;
  224.         var dbStatement = this.dbConn
  225.                 .createStatement("select guid, mediaUrl, filename, dir, referrer, size, status, percentComplete, amountTransferred, speed, downloadId from downloadings where guid = ?1");
  226.         dbStatement.bindInt32Parameter(0, guid);
  227.  
  228.         if (dbStatement.executeStep()) {
  229.             entry = {};
  230.             entry["guid"] = dbStatement.getString(0);
  231.             entry["mediaUrl"] = dbStatement.getString(1);
  232.             entry["filename"] = dbStatement.getString(2);
  233.             entry["dir"] = dbStatement.getString(3);
  234.             entry["referrer"] = dbStatement.getString(4);
  235.             entry["size"] = dbStatement.getInt64(5);
  236.             entry["status"] = dbStatement.getInt32(6);
  237.             entry["percentComplete"] = dbStatement.getInt32(7);
  238.             entry["amountTransferred"] = dbStatement.getInt64(8);
  239.             entry["speed"] = dbStatement.getDouble(9);
  240.             entry["downloadId"] = dbStatement.getInt32(10);
  241.         }
  242.  
  243.         dbStatement.finalize();
  244.  
  245.         return entry;
  246.     },
  247.     getDownloadingById : function(downloadId) {
  248.         //com.VidBar.__d("DB: getDownloadingById - "+downloadId);
  249.         this.inizialize();
  250.  
  251.         var entry = null;
  252.         var dbStatement = this.dbConn
  253.                 .createStatement("select guid, mediaUrl, filename, dir, referrer, size, status, percentComplete, amountTransferred, speed, downloadId from downloadings where downloadId = ?1");
  254.         dbStatement.bindInt32Parameter(0, downloadId);
  255.  
  256.         if (dbStatement.executeStep()) {
  257.             entry = {};
  258.             entry["guid"] = dbStatement.getString(0);
  259.             entry["mediaUrl"] = dbStatement.getString(1);
  260.             entry["filename"] = dbStatement.getString(2);
  261.             entry["dir"] = dbStatement.getString(3);
  262.             entry["referrer"] = dbStatement.getString(4);
  263.             entry["size"] = dbStatement.getInt64(5);
  264.             entry["status"] = dbStatement.getInt32(6);
  265.             entry["percentComplete"] = dbStatement.getInt32(7);
  266.             entry["amountTransferred"] = dbStatement.getInt64(8);
  267.             entry["speed"] = dbStatement.getDouble(9);
  268.             entry["downloadId"] = dbStatement.getInt32(10);
  269.         }
  270.  
  271.         dbStatement.finalize();
  272.  
  273.         return entry;
  274.     },
  275.     getDownloadingLength : function() {
  276.         this.inizialize();
  277.  
  278.         var len = 0;
  279.         var dbStatement = this.dbConn
  280.                 .createStatement("select count(*) as length from downloadings");
  281.         if (dbStatement.executeStep()) {
  282.             len = dbStatement.getInt32(0);
  283.         }
  284.         dbStatement.finalize();
  285.  
  286.         return len;
  287.     },
  288.     updateDownloadingProgress : function(guid, size, percentComplete,
  289.             amountTransferred, speed) {
  290.         this.inizialize();
  291.  
  292.         var dbStatement = this.dbConn
  293.                 .createStatement("update downloadings set size = ?1, percentComplete = ?2, amountTransferred = ?3, speed = ?4 where guid = ?5");
  294.         dbStatement.bindInt64Parameter(0, size);
  295.         dbStatement.bindInt32Parameter(1, percentComplete);
  296.         dbStatement.bindInt64Parameter(2, amountTransferred);
  297.         dbStatement.bindDoubleParameter(3, speed);
  298.         dbStatement.bindStringParameter(4, guid);
  299.  
  300.         dbStatement.execute();
  301.         dbStatement.finalize();
  302.     },
  303.     updateDownloadingStatus : function(guid, status) {
  304.         this.inizialize();
  305.  
  306.         var dbStatement = this.dbConn
  307.                 .createStatement("update downloadings set status = ?1 where guid = ?2");
  308.         dbStatement.bindInt32Parameter(0, status);
  309.         dbStatement.bindStringParameter(1, guid);
  310.  
  311.         dbStatement.execute();
  312.         dbStatement.finalize();
  313.     },
  314.     removeDownloading : function(guid) {
  315.         this.inizialize();
  316.  
  317.         var dbStatement = this.dbConn
  318.                 .createStatement("delete from downloadings where guid = ?1");
  319.         dbStatement.bindStringParameter(0, guid);
  320.  
  321.         dbStatement.execute();
  322.         dbStatement.finalize();
  323.     },
  324.     removeAllDownloadings : function() {
  325.         this.inizialize();
  326.         
  327.         //VidStatus = 4 (FAILED)
  328.         var dbStatement = this.dbConn
  329.                 .createStatement("delete from downloadings");
  330.                 
  331.         dbStatement.execute();
  332.         dbStatement.finalize();
  333.     },
  334.     addSite : function(name, url) {
  335.         this.inizialize();
  336.  
  337.         var dbStatement = this.dbConn
  338.                 .createStatement("insert into sites(name, url) values (?1, ?2)");
  339.         dbStatement.bindStringParameter(0, name);
  340.         dbStatement.bindStringParameter(1, url);
  341.  
  342.         dbStatement.execute();
  343.         dbStatement.finalize();
  344.     },
  345.     getSites : function() {
  346.         this.inizialize();
  347.  
  348.         var entries = [];
  349.         var dbStatement = this.dbConn
  350.                 .createStatement("select name, url from sites");
  351.  
  352.         while (dbStatement.executeStep()) {
  353.             var entry = {};
  354.             entry["name"] = dbStatement.getString(0);
  355.             entry["url"] = dbStatement.getString(1);
  356.             entries.push(entry);
  357.         }
  358.  
  359.         dbStatement.finalize();
  360.  
  361.         return entries;
  362.     },
  363.     removeSite : function(name) {
  364.         this.inizialize();
  365.  
  366.         var dbStatement = this.dbConn
  367.                 .createStatement("delete from sites where name = ?1");
  368.         dbStatement.bindStringParameter(0, name);
  369.  
  370.         dbStatement.execute();
  371.         dbStatement.finalize();
  372.     },
  373.     restoreDefaultSites : function() {
  374.         var defaultSites = [["Youtube", "http://www.youtube.com"],
  375.                 ["GoogleVideo", "http://video.google.com"],
  376.                 ["Myspace", "http://www.myspace.com"],
  377.                 ["Megavideo", "http://www.megavideo.com"],
  378.                 ["Break", "http://www.break.com"],
  379.                 ["DailyMotion", "http://www.dailymotion.com"],
  380.                 ["WatchMovies.net", "http://www.watch-movies-links.net"]];
  381.  
  382.         this.inizialize();
  383.  
  384.         this.dbConn.executeSimpleSQL("delete from sites");
  385.  
  386.         for (var i = 0; i < defaultSites.length; i++) {
  387.             this.addSite(defaultSites[i][0], defaultSites[i][1]);
  388.         }
  389.     },
  390.     startTransaction : function() {
  391.         if (!this.dbConn.transactionInProgress) {
  392.             this.dbConn.beginTransaction();
  393.         }
  394.     },
  395.     endTransaction : function() {
  396.         if (this.dbConn.transactionInProgress) {
  397.             this.dbConn.commitTransaction();
  398.         }
  399.     },
  400.     syncDownloads : function(dlMgr) {
  401.         var entries = this.getDownloadings();
  402.         com.VidBar.__d("VidDB.syncDownloads: " + entries.length);
  403.         for (var i = 0; i < entries.length; i++) {
  404.             var entry = entries[i];
  405.             try {
  406.                 com.VidBar.__d("download id: " + entry.downloadId);
  407.                 var download = dlMgr.getDownload(entry.downloadId);
  408.                 com.VidBar.__d("download: " + download);
  409.                 if (!download) {
  410.                     this.removeDownloading(entry.guid);
  411.                 } else {
  412.                     this.updateDownloadingProgress(entry.guid, download.size,
  413.                             download.percentComplete,
  414.                             download.amountTransferred, download.speed);
  415.                     this.updateDownloadingStatus(entry.guid, VidStatus
  416.                                     .translateState(download.state));
  417.                 }
  418.             } catch (e) {
  419.                 com.VidBar.__e("VidDB.syncDownloads: " + e);
  420.                 this.removeDownloading(entry.guid);
  421.             }
  422.         }
  423.     }
  424. };
  425.